<?php

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');#加载全站初始化配置文件
require_once (dirname(__FILE__) .'/includes/Response.php');#json生成类





//基本信息接口
if ($_REQUEST['act'] == 'basic_information') {
    $_POST['user_id'] = isset($_POST['user_id']) ? trim($_POST['user_id']) : '';
    $_POST['activity_id'] = isset($_POST['activity_id']) ? trim($_POST['activity_id']) : '';

    $sql = "SELECT `id` FROM " . $ecs->table('user_activity') . "WHERE user_id = '" . $_POST['user_id'] . "' " . "AND activity_id = '" . $_POST['activity_id'] ."'";
    $id = $db->getOne($sql);
    if (!empty($id)) {
        $sql = "SELECT user_name,user_sex,user_pic" .
            " FROM " . $ecs->table('user') .
            " WHERE  user_id = '" . $_POST['user_id'] . "' ";
        $row1 = $db->getRow($sql);//查询基本信息顶端的用户名，头像，性别
        //查询是否大v
        $sql = "SELECT id" .
            " FROM " . $ecs->table('dv') .
            " WHERE  user_id = '" . $_POST['user_id'] . "' AND status=1 ";
        $dv = $db->getRow($sql);
        if ($dv) {
            $row1['is_dv']=1;
        }else{
            $row1['is_dv']=1;
        }
    

        $sql = "SELECT activity_id,activity_name,activity_cover,activity_re_time,activity_address,fab_num,activity_describe" .
            " FROM " . $ecs->table('activity') .
            " WHERE  activity_id = '" . $_POST['activity_id'] . "' ";//查询活动名称，封面，发布时间，地址，点赞数，活动描述
        $row2 = $db->getRow($sql);

        $sql = "SELECT ecs_user.user_name,ecs_user.user_pic,ecs_user.user_id FROM " . $ecs->table('user_activity') . "LEFT JOIN " . $ecs->table('user') . "ON ecs_user.user_id = ecs_user_activity.user_id" . " WHERE ecs_user_activity.activity_id ='" . $_POST['activity_id'] . "'";
        $row3 = $db->getAll($sql);//查询活动参与者用户名，头像，ID

//        $sql = "select count(*) from".$ecs->table('like')."LEFT JOIN ".$ecs->table('user')."ON ecs_like.user_id=ecs_user.user_id"." WHERE ecs_like.activity_id ='" .$_POST['activity_id']."'";
        $row4 = count($row3);//计算参与者数量

        $now_time = strtotime(date("Y-m-d H:i:s"));//当前时间
        $re_time = strtotime($row2['activity_re_time']);//活动发布时间
        $time = $now_time - $re_time;//计算时间差

        $row = array(
            'top_information' => $row1,
            'middle_information' => $row2,
            'time_difference' => $time,
            'member_num' => $row4,
            'member' => $row3
        );

        return Response::json(200, "查询数据成功", $row);
    } else {
        return Response::json(450, "参与者与活动不匹配", $id);
    }
}

//影集展示图接口，获取最新一个影集
elseif($_REQUEST['act'] == 'picture_album'){
    $_POST['activity_id'] = isset($_POST['activity_id']) ? trim($_POST['activity_id']) : '';
    $sql = "select pa_id from".  $ecs->table('picture_album'). " where activity_id = '" . $_POST['activity_id'] ."' order by `pa_datetime` desc";
    $pa_id = $db->getOne($sql);
    if(!empty($pa_id)){
        $sql ="SELECT `picture_address` FROM " . $ecs->table('picture') . "WHERE pa_id = '" . $pa_id . "' ";
        $picture_album = $db->getAll($sql);
        return Response::json(200, "查询数据成功", $picture_album);
    }else{
        return Response::json(451, "影集不存在");
    }


}

//初始化点赞接口 判断用户是否点赞并返回所有点赞者对象
elseif($_REQUEST['act'] == 're_like'){
    $_POST['activity_id'] = isset($_POST['activity_id']) ? trim($_POST['activity_id']) : '';
    $_POST['user_id'] = isset($_POST['user_id']) ? trim($_POST['user_id']) : '';

    $sql = "SELECT `id` FROM " . $ecs->table('like') . "WHERE user_id = '" . $_POST['user_id'] . "' " . "AND activity_id = '" . $_POST['activity_id'] . "' ";
    $row = $db->getOne($sql);
    if(!empty($row)){
        $like = true;
    }else{
        $like = false;
    }

    $sql = "SELECT ecs_user.user_name,ecs_user.user_pic,ecs_user.user_id FROM " . $ecs->table('like') . "LEFT JOIN " . $ecs->table('user') . "ON ecs_like.user_id=ecs_user.user_id" . " WHERE ecs_like.activity_id ='" . $_POST['activity_id'] . "'";
    $row = $db->getAll($sql);

    $sql = "SELECT fab_num" . " FROM " . $ecs->table('activity') . " WHERE  activity_id = '" . $_POST['activity_id'] . "' ";
    $fab_num = $db->getOne($sql);
    $result = array(
        'like' => $like,
        'liker'=> $row,
        'fab_num' => $fab_num
    );

    return Response::json(200, "查询数据成功", $result);
}

//点赞接口，点赞或取消点赞
elseif($_REQUEST['act'] == 'like'){
    $_POST['activity_id'] = isset($_POST['activity_id']) ? trim($_POST['activity_id']) : '';
    $_POST['user_id'] = isset($_POST['user_id']) ? trim($_POST['user_id']) : '';
    $_POST['like'] = isset($_POST['like']) ? trim($_POST['like']) : '';

    if(empty($_POST['like'])){
        $like = false;
        $sql = "DELETE  FROM " . $ecs->table('like') . "WHERE user_id = '" . $_POST['user_id'] . "' " . "AND activity_id = '" . $_POST['activity_id'] . "' ";
        $db->query($sql);
        if(mysql_affected_rows()){
            $sql = "SELECT ecs_user.user_name,ecs_user.user_pic,ecs_user.user_id FROM " . $ecs->table('like') . "LEFT JOIN " . $ecs->table('user') . "ON ecs_like.user_id=ecs_user.user_id" . " WHERE ecs_like.activity_id ='" . $_POST['activity_id'] . "'";
            $row = $db->getAll($sql);
            $sql = "SELECT fab_num" .
                " FROM " . $ecs->table('activity') .
                " WHERE  activity_id = '" . $_POST['activity_id'] . "' ";
            $fab_num = $db->getOne($sql);
            $num = $fab_num - 1;
            $sql = "UPDATE" . $ecs->table('activity') . " SET fab_num =" . $num . " WHERE  activity_id = '" . $_POST['activity_id'] . "' ";
            $db->query($sql);
            $result = array(
                'like' => $like,
                'liker'=> $row,
                'fab_num' => $num
            );

            return Response::json(200, "取消点赞成功", $result);

        }else{
            return Response::json(452,"取消点赞失败");
        }

    }else{
        $like = true;
        $sql = "insert into". $ecs->table('like')." (activity_id,user_id) values('" .$_POST['activity_id']."','".$_POST['user_id']."')";
        $db->query($sql);
        if(mysql_affected_rows()){
            $sql = "SELECT ecs_user.user_name,ecs_user.user_pic,ecs_user.user_id FROM " . $ecs->table('like') . "LEFT JOIN " . $ecs->table('user') . "ON ecs_like.user_id=ecs_user.user_id" . " WHERE ecs_like.activity_id ='" . $_POST['activity_id'] . "'";
            $row = $db->getAll($sql);
            $sql = "SELECT fab_num" .
                " FROM " . $ecs->table('activity') .
                " WHERE  activity_id = '" . $_POST['activity_id'] . "' ";
            $fab_num = $db->getOne($sql);
            $num = $fab_num + 1;
            $sql = "UPDATE" . $ecs->table('activity') . " SET fab_num =" . $num . " WHERE  activity_id = '" . $_POST['activity_id'] . "' ";
            $db->query($sql);
            $result = array(
                'like' => $like,
                'liker'=> $row,
                'fab_num' => $num
            );

            return Response::json(200, "点赞成功", $result);

        }else{
            return Response::json(452,"点赞失败");
        }
    }
}


//评论列表接口
elseif($_REQUEST['act'] == 'review_list'){
    $_POST['activity_id'] = isset($_POST['activity_id']) ? trim($_POST['activity_id']) : '';

    $sql = "SELECT review_id,activity_id,user_id,reviewed_user_id,content,time,reviewed_id FROM " . $ecs->table('review') . " WHERE  activity_id = '".$_POST['activity_id']."' ORDER BY `time` ";//查询所有一级评论
    $row = $db->getAll($sql);
    if(!empty($row)) {
        foreach ($row as $value) {
            $sql = "SELECT user_name,user_sex,user_pic" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['user_id'] . "' ";
            $user = $row1 = $db->getRow($sql);
            $sql = "SELECT user_id,user_name" . " FROM " . $ecs->table('user') . " WHERE  user_id = '" . $value['reviewed_user_id'] . "' ";
            $reviewed_user = $db->getRow($sql);
            $re_time = strtotime($value['time']);
            $now_time = strtotime(date("Y-m-d H:i:s"));
            $time = $now_time - $re_time;
            $res = array(
                'review' => $value,
                'critics' => $user,
                'reviewed_user' => $reviewed_user,
                'time_difference' => $time,
            );

            $arr1[] = $res;


        }

        return Response::json(200, '评论列表拉取成功', $arr1);
    }else{
        return Response::json(453, '暂无评论', '');
    }
}

//评论接口
elseif($_REQUEST['act'] == 'review'){
    require(dirname(__FILE__) . '/includes/Jpush.php');
    $_POST['activity_id'] = isset($_POST['activity_id']) ? trim($_POST['activity_id']) : '';
    $_POST['user_id'] = isset($_POST['user_id']) ? trim($_POST['user_id']) : '';
    $_POST['reviewed_user_id'] = isset($_POST['reviewed_user_id']) ? trim($_POST['reviewed_user_id']) : '';
    $_POST['content'] = isset($_POST['content']) ? trim($_POST['content']) : '';
    $_POST['reviewed_id'] = isset($_POST['reviewed_id']) ? trim($_POST['reviewed_id']) : '';


//    if(empty($_POST['reviewed_id'])){

        $time = date("Y-m-d H:i:s");
        $sql = "insert into ". $ecs->table('review'). " (activity_id,user_id,reviewed_user_id,content,time) values('" .$_POST['activity_id']."','".$_POST['user_id']."','".$_POST['reviewed_user_id']."','".$_POST['content']."','".$time."')";
        $db->query($sql);
        if(mysql_affected_rows()){
            $sql = "SELECT * FROM ".$ecs->table('review'). "WHERE user_id = '".$_POST['user_id']."' AND time = '".$time."'";
            $row = $db->getRow($sql);
            $sql = "insert into " . $ecs->table('message_text') . " (sender_id,message_text) values('" . $_POST['user_id'] . "','" . $_POST['content'] . "')";
            $row1 = $db->query($sql);
            $sql = "SELECT LAST_INSERT_ID()";
            $message_text_id = $db->getOne($sql);
            $sql = "insert into " . $ecs->table('message') . " (sender_id,receiver_id,send_time,message_text_id) values('" . $_POST['user_id'] . "','" . $_POST['reviewed_user_id'] .  "','". $time . "','" . $message_text_id . "')";
            $row2 = $db->query($sql);
            //推送
            $pushObj = new Jpush();
            $sql = "SELECT ecs_message.*,ecs_message_text.* FROM ".$ecs->table('message')."INNER JOIN " .$ecs->table('message_text')." ON ecs_message.message_text_id = ecs_message_text.message_text_id WHERE ecs_message.receiver_id = " .$_POST['reviewed_user_id']. " ORDER BY send_time DESC";
            $message = $db->getRow($sql);
            $sql = "SELECT user_name FROM ".$ecs->table('user')." WHERE user_id = " .$_POST['user_id'] ;
            $sender_name = $db->getOne($sql);
            $push_content = array(
                'sender_name' => $sender_name,
                'message' => $message
            );
            $content = json_encode($push_content);
            $sql = "SELECT registration_id FROM ".$ecs->table('registration_id')." WHERE user_id = " .$_POST['reviewed_user_id'] ;
            $registration_id = $db->getOne($sql);
            $receive = array('registration_id'=>array($registration_id));
            $result = $pushObj->push($receive,$content);
            if($result){
                $res_arr = json_decode($result, true);
                if(isset($res_arr['error'])) {                       //如果返回了error则证明失败
                    echo $res_arr['error']['message'];          //错误信息
                    echo $res_arr['error']['code'];             //错误码
                    return false;
                }
            }else{      //接口调用失败或无响应
                echo '推送接口调用失败或无响应';
                return false;
            }

            return Response::json(220,'评论成功',$row);
        }else{
            return Response::json(454,'评论失败');
        }
//    }else{
//
//        $sql = "SELECT `reviewed_id` FROM ".$ecs->table('review'). "WHERE review_id = '".$_POST['reviewed_id']."'";
//        $reviewed_id = $db->getOne($sql);
//        if(empty($reviewed_id)){
//            $time = date("Y-m-d H:i:s");
//            $sql = "insert into ". $ecs->table('review'). " (activity_id,user_id,reviewed_user_id,content,time,reviewed_id) values('" .$_POST['activity_id']."','".$_POST['user_id']."','".$_POST['reviewed_user_id']."','".$_POST['content']."','".$time."','".$_POST['reviewed_id']."')";
//            $db->query($sql);
//            if(mysql_affected_rows()){
//                $sql = "SELECT * FROM ".$ecs->table('review'). "WHERE user_id = '".$_POST['user_id']."' AND time = '".$time."'";
//                $row = $db->getRow($sql);
//                return Response::json(220,'评论成功',$row);
//            }else{
//                return Response::json(454,'评论失败');
//            }
//        }else{
//            $time = date("Y-m-d H:i:s");
//            $sql = "insert into ". $ecs->table('review'). " (activity_id,user_id,reviewed_user_id,content,time,reviewed_id) values('" .$_POST['activity_id']."','".$_POST['user_id']."','".$_POST['reviewed_user_id']."','".$_POST['content']."','".$time."','".$reviewed_id."')";
//            $db->query($sql);
//            if(mysql_affected_rows()){
//                $sql = "SELECT * FROM ".$ecs->table('review'). "WHERE user_id = '".$_POST['user_id']."' AND time = '".$time."'";
//                $row = $db->getRow($sql);
//                return Response::json(220,'评论成功',$row);
//            }else{
//                return Response::json(454,'评论失败');
//            }
//        }
//
//    }


}

//参与者列表接口
elseif($_REQUEST['act'] == 'member_list') {
    $_POST['activity_id'] = isset($_POST['activity_id']) ? trim($_POST['activity_id']) : '';
    $sql = "SELECT ecs_user.user_name,ecs_user.user_pic,ecs_user.user_id FROM " . $ecs->table('user_activity') . "LEFT JOIN " . $ecs->table('user') . "ON ecs_user.user_id = ecs_user_activity.user_id" . " WHERE ecs_user_activity.activity_id ='" . $_POST['activity_id'] . "' order by convert(ecs_user.user_name using gbk) asc;";
    $row = $db->getAll($sql);
    if (mysql_affected_rows()) {
        return Response::json(200, '获取参与者列表成功', $row);
    }else{
        return Response::json(455, '获取参与者列表失败');
    }
}

//发送消息

//
//elseif($_REQUEST['act'] == 'send') {
//$_POST['sender_id'] = isset($_POST['sender_id']) ? trim($_POST['sender_id']) : '';
//$_POST['receiver_id'] = isset($_POST['receiver_id']) ? trim($_POST['receiver_id']) : '';
//$_POST['message_text'] = isset($_POST['message_text']) ? trim($_POST['message_text']) : '';
//
//$_POST['sender_id'] = 1;
//$_POST['receiver_id'] = 2;
//$_POST['message_text'] = 'sdfsdff';
//    if(empty($_POST['message_text'])){
//
//        return Response::json(456, "消息内容不能为空");
//
//    }else{
//
//        $sql = "insert into " . $ecs->table('message_text') . " (sender_id,message_text) values('" . $_POST['sender_id'] . "','" . $_POST['message_text'] . "')";
//        $row1 = $db->query($sql);
//        $sql = "SELECT LAST_INSERT_ID()";
//        $message_text_id = $db->getOne($sql);
//        $send_time = date("Y-m-d H:i:s");
//        $sql = "insert into " . $ecs->table('message') . " (sender_id,receiver_id,send_time,message_text_id) values('" . $_POST['sender_id'] . "','" . $_POST['receiver_id'] . "','" . $send_time . "','" . $message_text_id . "')";
//        $row2 = $db->query($sql);
//        return Response::json(200, "消息发送成功");
//    }
//
//}
//



/**
 * Created by PhpStorm.
 * User: tuhao
 * Date: 2016/11/1
 * Time: 10:26
 */